package multithreading;

public class Demo9_Priority {


    public static void main(String[] args) {

        //创建五个非共享资源的线程
        Thread thr_a = new Thread(new PrintNumber(),"A:10");
        Thread thr_b = new Thread(new PrintNumber(),"B:10");
        Thread thr_c = new Thread(new PrintNumber(),"C:10");
        Thread thr_d = new Thread(new PrintNumber(),"D:10");
        Thread thr_e = new Thread(new PrintNumber(),"E:1");

        //分别设置他们的优先级
        thr_a.setPriority(10);
        thr_b.setPriority(10);
        thr_c.setPriority(10);
        thr_d.setPriority(10);
        thr_e.setPriority(1);

        thr_a.start();
        thr_b.start();
        thr_c.start();
        thr_d.start();
        thr_e.start();
    }

}

class PrintNumber implements Runnable{

    int sum = 1000;

    @Override
    public void run() {

        while(true){
            if(this.sum<0){
                break;
            }
            //每个线程内，没循环一次，输出一个数字
            System.out.println(Thread.currentThread().getName()+":"+(this.sum--));
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //某个线程执行完毕后，输出一次
        System.out.println(Thread.currentThread().getName()+"执行完毕");
    }
}
